import api from '@/request/api.js';

// uni.login()封装
const wxLogin = function(openid) {
	return new Promise((resolve, reject) => {
		uni.login({
			success: res => {
				// 获取sessionkey，openid
				api.getSessionKey({
					'js_code': res.code
				}).then(res => {
					console.log('登录信息接口返回:', res)
					uni.hideLoading();
					let phone=uni.getStorageSync('phone')
					uni.setStorageSync("useinfo", res.userInfo);
					uni.setStorageSync("openid", res.data.openid);
					/* 给后台返回用户信息 */
					
				}).catch(err => {
					console.log(err)
				})
				console.log(res)
				if (res.code) {
					resolve(res.code)
					// 

				} else {
					reject(res.errMsg);
				}
			}

		})
	})
}
/*微信小程序登录*/
const wechatAppLogin = function() {
	/*登录提示*/

	uni.showLoading("正在授权")
	uni.getUserProfile({
		desc: '获取用户授权',
		success: res => {
			console.log(res.userInfo)
			let userInfo = res.userInfo;
			let openid=uni.getStorageSync('openid')
			wxLogin().then(res => {
				// 引用uni.login()封装
				console.log(res)
				/* 获取成功给后台返回用户信息 */
				api.setUserInfo({
					'avatarUrl': userInfo.avatarUrl,
					'gender': userInfo.gender,
					'nickName': userInfo.nickName,
					'openid': openid
				}).then(res => {
					console.log(res)
					/* 存全局变量 */
					getApp().globalData.nickName = res.data.nickname
					getApp().globalData.avatarUrl = res.data.avatar
					getApp().globalData.is_auth = res.data.is_auth
					getApp().globalData.token = res.data.token
					getApp().globalData.uid = res.data.uid
					getApp().globalData.nft_address = res.data.nft_address
					console.log(res.data.is_auth)
					//将用户信息存入本地缓存
					uni.setStorage({
						key: 'userInfo',
						data: {
							'openid': openid,
							'nickName': res.data.nickname,
							'avatarUrl': res.data.avatar,
							'token': res.data.token,
							'is_auth': res.data.is_auth,
							'uid': res.data.uid,
							'mobile': res.data.mobile,
							'update_time': res.data.update_time,
							'nft_address': res.data.nft_address
						}
					});
					/* 判断用户是否实名认证 */
					/* 更改用户登陆状态 */
					uni.setStorageSync('hasLogin',true)
					/* 设置延迟返回首页 */
					if(res.code==true){
						/* 跳tabbar页面使用 */
						uni.switchTab({
							url:'/pages/index/index'
						})
					}
				
				}).catch(err => {
					/* console.log(err) */
				
				})
				

				/* myRequest('getOpenid', 'POST', {
						code: code
					}) //获取openid
					.then(function(v) {
						uni.hideLoading();
						uni.setStorageSync("useInfo", res.userInfo);
						uni.setStorageSync("openid", v.openid);
						wx.navigateTo({
							url: '/pages/login/index'
						})
					}, function(error) {
						reject(error);
					}) */

			})
		}
	})
}

// 获取手机号授权
const getPhoneNumber = function(event) {
	console.log(111)
	let that = this;
	let code = event.detail.code; //获取手机code
	var promise = new Promise(function(resolve, reject) {
		console.log(555)
		wxLogin().then(code=>{
			uni.checkSession({
				success: (res) => {
					console.log(res)
					/* 授权弹框 */
					uni.showModal({
						content: '是否授权用户头像昵称',
						success: res => {
							if (res.confirm) {
								console.log('用户点击确定');
								wechatAppLogin()
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
					//给后台返回解析手机号的code
			
					/* api('getPhone', 'POST', {
							code: code
						}) //获取手机号
						.then(function(v) {
			
							let phone = v.data;
							let useInfo = uni.getStorageSync('useInfo')
							wx.setStorageSync('mobile', mobile)
							resolve(phone);
							
						}, function(error) {
							reject(error);
						}) */
						
				},
				fail(err) {
					console.log(err)
					wxLogin().then(code=>{
						getPhoneNumber()
					})
				}
			})
		})
		
	})
	return promise;
}

module.exports = {
	wechatAppLogin,
	getPhoneNumber
}
